package com.chen.controller;

import com.chen.constant.MessageConstant;
import com.chen.entity.PageResult;
import com.chen.entity.QueryPageBean;
import com.chen.entity.Result;
import com.chen.pojo.CheckItem;
import com.chen.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author zhebie
 * @DATE 2021/9/16
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    //注入service
    @Autowired
    private CheckItemService checkItemService;

    /**
     * 查询所有
     */
    @GetMapping("/findAll")
    public Result findAll(){
        //调用业务查询
        List<CheckItem> checkItemList = checkItemService.findAll();
        //返回查询结果
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
    }

    /**
     * 新增检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result add(@RequestBody CheckItem checkItem){
        //调用业务增加
        checkItemService.add(checkItem);
        //返回结果
        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<CheckItem> pageResult = checkItemService.findPage(queryPageBean);
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable int id){
        checkItemService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 通过id查询检查项
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable int id){
        CheckItem checkItem = checkItemService.findById(id);
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
    }

    /**
     * 修改检查项 - 提交
     * @param id
     * @param checkItem
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable int id,@RequestBody CheckItem checkItem){
        //按restful规范，id以路径上的参数为准
        checkItem.setId(id);
        checkItemService.update(checkItem);
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    @GetMapping("/test")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result test(){
        return new Result(true,MessageConstant.ADD_ENABLE);
    }
}
